package com.nn.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.nn.mapper.DomesticTravelMapper;
import com.nn.pojo.DomesticTravel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DomesticTravelImpl implements DomesticTravelService {

    @Autowired
    private DomesticTravelMapper domesticTravelMapper;

    public Map<Integer, List<DomesticTravel>> domesticTravelMap() {
        Map<Integer, List<DomesticTravel>> map = new HashMap<>();
        List<DomesticTravel> list = domesticTravelMapper.selectList(null);
        for (DomesticTravel domesticTravel : list) {
            int parentId = domesticTravel.getParentId();
            if (map.containsKey(parentId)) {
                List<DomesticTravel> exeList = map.get(parentId);
                exeList.add(domesticTravel);
            } else {
                List<DomesticTravel> firstList = new ArrayList<>();
                firstList.add(domesticTravel);
                map.put(parentId, firstList);
            }
        }
        return map;
    }

    public List<DomesticTravel> getTwoList(Map<Integer, List<DomesticTravel>> map) {
        List<DomesticTravel> oneList = map.get(0);
        for (DomesticTravel oneItemCat : oneList) {
            List<DomesticTravel> twoList = map.get(oneItemCat.getId());
            oneItemCat.setChildren(twoList);
        }
        return oneList;
    }


    @Override
    public List<DomesticTravel> finddomesticTravelList(Integer level) {
        long startTime = System.currentTimeMillis();
        Map<Integer, List<DomesticTravel>> map = domesticTravelMap();
        if (level == 1) {
            return map.get(0);
        }
        if (level == 2) {
            return getTwoList(map);
        }
        List<DomesticTravel> oneList = getTwoList(map);
        for (DomesticTravel oneItemCat : oneList) {
            List<DomesticTravel> twoList = oneItemCat.getChildren();
            if (twoList == null || twoList.size() == 0) {
                continue;
            }
            for (DomesticTravel twoItemCat : twoList) {
                List<DomesticTravel> threeList = map.get(twoItemCat.getId());
                twoItemCat.setChildren(threeList);
            }
        }
        long endTime = System.currentTimeMillis();
        System.out.println("耗时:" + (endTime - startTime) + "毫秒");
        return oneList;
    }


    @Override
    @Transactional
    public void updateStatus(DomesticTravel domesticTravel) {
        domesticTravelMapper.updateById(domesticTravel);
    }

    @Override
    @Transactional
    public void adddomesticTravelForm(DomesticTravel domesticTravel) {
        domesticTravel.setStatus(true);
        domesticTravelMapper.insert(domesticTravel);
    }

    @Override
    @Transactional
    public void updatedomesticTravel(DomesticTravel domesticTravel) {
        domesticTravelMapper.updateById(domesticTravel);
    }

    @Override
    @Transactional
    public void deletedomesticTravel(DomesticTravel domesticTravel) {
        if (domesticTravel.getLevel() == 3) {
            domesticTravelMapper.deleteById(domesticTravel.getId());
            return;
        }
        if (domesticTravel.getLevel() == 2) {
            QueryWrapper<DomesticTravel> queryWrapper = new QueryWrapper();
            queryWrapper.eq("parent_id", domesticTravel.getId());
            domesticTravelMapper.delete(queryWrapper);
            domesticTravelMapper.deleteById(domesticTravel.getId());
            return;
        }
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("parent_id", domesticTravel.getId());
        List<DomesticTravel> twolist = domesticTravelMapper.selectList(queryWrapper);
        for (DomesticTravel twoDomestic : twolist) {
            queryWrapper.clear();
            queryWrapper.eq("parent_id", twoDomestic.getId());
            domesticTravelMapper.delete(queryWrapper);
            domesticTravelMapper.deleteById(twoDomestic.getId());
        }
        domesticTravelMapper.deleteById(domesticTravel.getId());
    }
}
